package backtrack;

import java.util.ArrayList;
import java.util.List;

public class Solution_39 {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer>path=new ArrayList<>();
        dfs(candidates,0,target,path);
        return  res;
    }

    private void dfs(int[] candidates, int i, int target, List<Integer> path) {
        if(target<0){
            return ;
        }
        if(target==0){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int j = i; j < candidates.length; j++) {
            path.add(candidates[j]);
            target-=candidates[j];
            dfs(candidates,j,target,path);
            target+=candidates[i];
            path.remove(path.size()-1);
        }
    }
}
